
/**
 * 去除字符串中的所有html标签和前后的空格
 * 
 * @param {any} txt 
 * @returns 
 */
const removeHtmlTag = function (txt) {
    return txt.replace(/<[^>]+>/g, "").replace(/(^\s*)|(\s*$)/g, "")
}

const fileAlias = {
    zip: 'zip',
    rar: 'archive',
    '7z': 'archive',
    mp3: 'audio',
    wma: 'audio',
    wav: 'audio',
    asf: 'audio',
    aac: 'audio',
    ogg: 'audio',
    xls: 'excel',
    xlsx: 'excel',
    bmp: 'image',
    gif: 'image',
    jpg: 'image',
    jpeg: 'image',
    svg: 'image',
    psd: 'image',
    png: 'image',
    cdr: 'image',
    ai: 'image',
    'mpeg-1': 'movie',
    'mpeg-2': 'movie',
    mpeg: 'movie',
    mpeg4: 'movie',
    mp4: 'movie',
    avi: 'movie',
    rmvb: 'movie',
    rm: 'movie',
    mov: 'movie',
    pdf: 'pdf',
    ppt: 'powerpoint',
    pptx: 'powerpoint',
    txt: 'text',
    log: 'text',
    bat: 'text',
    doc: 'word',
    docx: 'word'
}
/**
 * 
 * 匹配fontAwesome icon
 * @param {any} ext 文件格式
 * @returns 'icon name'
 */
const getFileIcon = function (ext) {
    ext = ext.toLowerCase();
    return fileAlias[ext] ? 'file-'+fileAlias[ext] : 'file'
}

/**
 * 日期对象格式化
 * 传入date对象或日期字符串或时间戳
 * @param {any} date 
 * @param {any} type 
 * @returns 
 */

const week = { 1: "一", 2: "二", 3: "三", 4: "四", 5: "五", 6: "六", 0: "日" }

const dateFormat = function (date, type) {
    if (typeof date == 'string' || typeof date == 'number') {
        date = new Date(date)
    }
    let Y = date.getFullYear(),
        m = (date.getMonth() + 1) >= 10 ? (date.getMonth() + 1) : '0' + (date.getMonth() + 1),
        d = date.getDate() >= 10 ? date.getDate() : '0' + date.getDate(),
        H = date.getHours() >= 10 ? date.getHours() : '0' + date.getHours(),
        i = date.getMinutes() >= 10 ? date.getMinutes() : '0' + date.getMinutes(),
        w = `星期${week[date.getDay()]}`
    if (type == 'hm') {
        return `${H}:${i}`;
    } else if (type == 'ymd') {
        return `${Y}-${m}-${d}`;
    } else if (type == 'ymdhm') {
        return `${Y}-${m}-${d} ${H}:${i}`;
    } else if (type == 'w') {
        return w
    } else if (type == 'ymdc') {
        return `${Y}年${m}月${d}日`;
    }
}
/**
 * 获取一个dom元素的兄弟元素
 * 
 * @param {any} elm 
 * @returns 
 */
const siblings = function (elm) {
    var a = [];
    var p = elm.parentNode.children;
    for (var i = 0, pl = p.length; i < pl; i++) {
        if (p[i] !== elm) a.push(p[i]);
    }
    return a;
}
/**
 * 文件大小转化
 * 
 * @param {any} num 
 * @returns 
 */
const fileSizeTransform = function (num) {
    if (num / 1024 < 1024) {
        return `${(num / 1024).toFixed(2)}K`
    } else if ((num / 1024) / 1024 < 1024){
        return `${((num / 1024) / 1024).toFixed(2)}M`
    } else {
        return `${((num / 1024) / 1024 / 1024).toFixed(2)}G`
    }
}

export {
    dateFormat,
    getFileIcon,
    removeHtmlTag,
    siblings,
    fileSizeTransform
}